package cn.wolfcode.contorller;

import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyTheme;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/strategy")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    
    //查询全部列表
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyQuery query){
        Page<Strategy> page = strategyService.query(query);
        model.addAttribute("page",page);
        return "strategy/list";
    }
    //跳转添加与编辑页面
    @RequestMapping("/input")
    public  String input(Model model,String id){
        //编辑回显
        if (StringUtils.hasLength(id)){
            Strategy strategy = strategyService.get(id);
            model.addAttribute("strategy",strategy);
        }
        
        /*分类*/
        //List<StrategyCatalog> list = strategyCatalogService.list();
        List<CatalogVO>list =strategyCatalogService.getCatalogs();
        model.addAttribute("catalogs",list);
        /*主题*/
        List<StrategyTheme> list1 = strategyThemeService.list();
        model.addAttribute("themes",list1);
        
        /*添加直接跳转*/
        return "strategy/input";
    }
    //添加与编辑的方法
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){
        //保存与编辑保存方法
        strategyService.saveOrUpdate(strategy);
        //返回一个json
        return JsonResult.success();
    }
    



    
    //删除方法
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        strategyService.delete(id);
        return JsonResult.success();
    }
    
    //更改发布状态
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(String id , int state){
        strategyService.changeState(id,state);
        return JsonResult.success();
    }
}
